package pkg;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class BaseMenu {

	public static String ADDRES = "base_index.jsp";
	public BaseMenu() {

	}

	public static String printBaseMenu(HttpSession session) {
		StringBuffer out = new StringBuffer();
		if (session.getAttribute("user") != null) {
			out.append("<li id=\"menu-01\"><a href=\"base_index.jsp?wybor=luser\">"
							+ "Lista uzytkownikow</a></li>\n");
			out.append("<li id=\"menu-02\"><a href=\"base_index.jsp?wybor=nuser\">"
							+ "Nowy uzytkownik</a></li>\n");
			out.append("<li id=\"menu-03\"><a href=\"base_index.jsp?wybor=lgrupy\">"
							+ "Lista grup</a></li>\n");
			out.append("<li id=\"menu-04\"><a href=\"base_index.jsp?wybor=ngrupy\">"
							+ "Nowa grupa</a></li>\n");

			out.append("<p></p>");
			out.append("<li id=\"menu-07\"><a href=\"base_index.jsp\">Regulamin</a>"
							+ "</li><li id=\"menu-08\"><a href=\"kontakt.jsp\">Kontakt</a></li>");
		}
		return out.toString();
	}
	
	public static String printView(HttpSession session, HttpServletRequest request) {
		BaseEngine engine = (BaseEngine) session.getAttribute("Baza");
		
		StringBuffer out = new StringBuffer();
		String wybor =  request.getParameter("wybor");
		if (wybor == null)
			return new String();
		
		if (wybor.equals("nuser"))
			return printUserForm("new_user2", engine);
		else if (wybor.equals("new_user2")){
			if (BaseService.addNewUser(request.getParameter("name"),
					request.getParameter("index_nr"), 
					request.getParameter("email"), 
					request.getParameter("grupa"), engine))
				return "<h2>Dodano nowego uzytkownika</h2>";
			else
				return "<h2>Wystapil blad - nie dodano uzytkownika</h2>";
		}
		else if (wybor.equals("luser"))
			return BaseService.printUserList(engine);
		else if (wybor.equals("ngrupy"))
			return printGroupForm("new_group2");
		else if (wybor.equals("new_group2")){
			if (BaseGroupService.addNewGroup(request.getParameter("name"),
					request.getParameter("comment"),
					engine))
				return "<h2>Dodano nowa grupe</h2>";
			else
				return "<h2>Wystapil blad - nie dodano grupy</h2>";
		}
		else if (wybor.equals("lgrupy"))
			return BaseGroupService.printGroupList(engine);
		
		out.append(wybor);
		return out.toString();
	}
	
	private static String printGroupForm(String dest){
		StringBuffer out = new StringBuffer();
		out.append("<FORM METHOD=POST ACTION=\""+ADDRES+"?wybor="+dest+"\">");
		out.append("<TABLE border=1>");
		
		out.append("<TR>");
		out.append("<TD width = 100>Nazwa:</TD>");
		out.append("<TD><INPUT TYPE=TEXT NAME=name SIZE=20></TD>");
		out.append("<TR>");
		
		out.append("<TR>");
		out.append("<TD width = 100>Komentarz:</TD>");
		out.append("<TD><INPUT TYPE=TEXT NAME=comment SIZE=20></TD>");
		out.append("<TR>");
		
		out.append("</TABLE>");
		
		out.append("<P align=\"center\"><INPUT TYPE=submit value=\"Dodaj\"></P>");
		out.append("</FORM>");
		
		return out.toString();
	}
	
	private static String printUserForm(String dest, BaseEngine engine){
		StringBuffer out = new StringBuffer();
		out.append("<FORM METHOD=POST ACTION=\""+ADDRES+"?wybor="+dest+"\">");
		out.append("<TABLE border=1>");
		
		out.append("<TR>");
		out.append("<TD width = 100>Nazwisko Imie:</TD>");
		out.append("<TD><INPUT TYPE=TEXT NAME=name SIZE=20></TD>");
		out.append("<TR>");
		
		out.append("<TR>");
		out.append("<TD width = 100>Numer indeksu:</TD>");
		out.append("<TD><INPUT TYPE=TEXT NAME=index_nr SIZE=20></TD>");
		out.append("<TR>");
		
		out.append("<TR>");
		out.append("<TD width = 100>E-mail:</TD>");
		out.append("<TD><INPUT TYPE=TEXT NAME=email SIZE=20></TD>");
		out.append("<TR>");
		
		out.append("<TR>");
		out.append("<TD width = 100>Grupa:</TD>");
		out.append("<TD><SELECT NAME=grupa SIZE=7>");
		out.append(BaseGroupService.printGroupOptions(engine));
		out.append("</TD>");
		out.append("<TR>");
		
		out.append("</TABLE>");
		
		out.append("<P align=\"center\"><INPUT TYPE=submit value=\"Dodaj\"></P>");
		out.append("</FORM>");
		
		return out.toString();
	}
}
